<!--
 * @description  :运输情报管理
 * @descriptionDetail:运输情报管理
 * @copyright    : 浙江烟草
 * @author       : dw
 * @create       : 2022-10-09 22:21:21
-->
<template>
  <view>
    <!-- #ifdef H5-ONLY -->
    <theme-navbar :is-white="false" :title="navTitle" :custom-back="goBack" style="z-index: 99999">
    </theme-navbar>
    <!-- #endif -->

    <view class="nolicence-search-lay" :style="{'top': isHideNav ? 0 : '44px'}">
      <view class="tab-lay">
        <u-tabs :is-scroll="false" :list="tabList" :current="currentTab" @change="tabChange"></u-tabs>
      </view>
      <view class="nolicenSearch">
        <u-search
          shape="round"
          :clearabled="true"
          :showAction="true"
          actionText="确定"
          :animation="true"
          :placeholder="plTitle"
          v-model="searchKey"
          @custom="toSearchList"
        ></u-search>
      </view>
    </view>

    <view class="content u-page">
      <me-table :i="0" :index="0" :top="120" :load-data="loadData" :height="getListViewHeight()" :init-load="false" ref="mescrollItem0">
        <view slot-scope="d" class="list-item-root">
          <u-swipe-action
            :show="d.data.show"
            :index="d.data.uuid"
            :disabled="d.data.swiperDisabled"
            @click="swiperClick"
            @open="swiperOpen"
            :options="d.data.swiperOptions"
          >
            <view class="list-lay">
              <list-item-logistics v-if="currentTab == 0" :itemInfo="d.data"></list-item-logistics>
              <list-item-express v-else-if="currentTab == 1" :itemInfo="d.data"></list-item-express>
              <list-item-facsimile v-else-if="currentTab == 2" :itemInfo="d.data"></list-item-facsimile>
              <list-item-road-transport v-else-if="currentTab == 3" :itemInfo="d.data"></list-item-road-transport>
              <list-item-rail-road-transport v-else-if="currentTab == 4" :itemInfo="d.data"></list-item-rail-road-transport>
              <list-item-air-transport v-else-if="currentTab == 5" :itemInfo="d.data"></list-item-air-transport>
              <list-item-waterway-transport v-else-if="currentTab == 6" :itemInfo="d.data"></list-item-waterway-transport>
            </view>
          </u-swipe-action>
        </view>
      </me-table>
    </view>
    <view>
      <u-action-sheet :list="tabList" v-model="addMenuShow" @click="gridClick"></u-action-sheet>
    </view>
    <view class="submit-btns btn-other-cls">
      <u-button class="u-btn" type="primary" @click="addMenu">
        添加
      </u-button>
    </view>
  </view>
</template>

<script>
import MescrollMoreMixin from "@/components/mescroll-uni/mixins/mescroll-more.js";
import meTable from "@/components/me-table.vue";
import ListItemLogistics from "./components/listItemLogistics.vue";
import ListItemExpress from "./components/listItemExpress.vue";
import ListItemFacsimile from "./components/listItemFacsimile.vue";
import ListItemRoadTransport from "./components/listItemRoadTransport.vue";
import ListItemRailRoadTransport from "./components/listItemRailRoadTransport.vue";
import listItemAirTransport from "./components/listItemAirTransport.vue";
import ListItemWaterwayTransport from "./components/listItemWaterwayTransport.vue";
import intelligenceservice from "@/service/intelligence/intelligence.service";
export default {
  components: {
    meTable,
    ListItemLogistics,
    ListItemFacsimile,
    ListItemExpress,
    ListItemRoadTransport,
    ListItemRailRoadTransport,
    listItemAirTransport,
    ListItemWaterwayTransport,
  },
  mixins: [MescrollMoreMixin,],
  data() {
    return {
      addMenuShow: false,
      tabList: [
        { id: 1, icon: "wuliu2", name: "物流", text: "物流", path: "pages/intelligence/intelligenceFilesAdd/logisticsArchive", },
        { id: 2, icon: "kuaidi", name: "快递", text: "快递", path: "pages/intelligence/intelligenceFilesAdd/expressArchive", },
        { id: 3, icon: "quanliqingdan", name: "面单", text: "面单", path: "pages/intelligence/intelligenceFilesAdd/facsimileArchives", },
        { id: 4, icon: "gaosu", name: "公路", text: "公路", path: "pages/intelligence/intelligenceFilesAdd/roadtransportArchive", },
        { id: 5, icon: "tielu", name: "铁路", text: "铁路", path: "pages/intelligence/intelligenceFilesAdd/railroadtransportArchive", },
        { id: 6, icon: "kongyun", name: "航空", text: "航空", path: "pages/intelligence/intelligenceFilesAdd/airtransportArchive", },
        { id: 7, icon: "chuanzhi", name: "水路", text: "水路", path: "pages/intelligence/intelligenceFilesAdd/waterwaytransportArchive", },
      ],
      currentTab: 0,
      searchKey: "",
      customStyle: {
        padding: "30rpx",
      },
      user: this.$storage.get(this.$rp.common.USER),
      plTitle: "物流公司名称/地址",
      defaultOrg: undefined,
      orgName: "",
      orgUuid: "",
    };
  },
  methods: {
    getListViewHeight() {
      let p = this.$u.sys().windowHeight - 44 - 57 - 40 - uni.upx2px(10) - this.$u.sys().statusBarHeight + "px";
      // #ifdef H5-DINGDING || H5-WECHAT
      p = this.$u.sys().windowHeight - uni.upx2px(10) - 57 - 40 - this.$u.sys().statusBarHeight + "px";
      // #endif
      return p;
    },
    tabChange(n) {
      switch (n) {
      case 0:
        this.plTitle = "物流公司/地址/承运路线";
        break;
      case 1:
        this.plTitle = "快递公司/网点";
        break;
      case 2:
        this.plTitle = "面单号码";
        break;
      case 3:
        this.plTitle = "车牌号码/汽车品牌";
        break;
      case 4:
        this.plTitle = "班次名/铁路局";
        break;
      case 5:
        this.plTitle = "班次名";
        break;
      case 6:
        this.plTitle = "班次名";
        break;
      }
      this.currentTab = n;
      this.searchKey = "";
      // // TODO 是否需要滚动到top
      this.$refs.mescrollItem0.listDatas = [];
      this.getMescroll(0).triggerDownScroll();
    },
    toSearchList(e) {
      this.getMescroll(0).triggerDownScroll();
    },
    addMenu() {
      this.addMenuShow = true;
    },
    // 新增
    gridClick(i) {
      this.addMenuShow = false;
      if (this.$u.test.isEmpty(i) && i != 0) {
        this.$u.toast("功能开发中...");
        return;
      }
      this.$u.route({
        url: this.tabList[i].path,
        params: {
          from: "add",
          orgName: this.orgName,
          orgUuid: this.orgUuid,
        },
      });
    },
    loadData(page) {
      let params = {
        page: page.num,
        limit: page.size,
        paras: {},
      };
      let fun = "";
      return new Promise((resolve, reject) => {
        switch (this.currentTab) {
        case 0:
          {
            params.paras.fuzzyQuery = this.searchKey;
            fun = intelligenceservice.logisticsPage;
          }
          break;
        case 1:
          {
            params.paras.fuzzyQuery = this.searchKey;
            fun = intelligenceservice.expressInfoPage;
          }
          break;
        case 2:
          {
            params.paras.billCode = this.searchKey;
            fun = intelligenceservice.billInfoPage; // para
          }
          break;
        case 3:
          {
            params.paras.fuzzyQuery = this.searchKey;
            fun = intelligenceservice.carInfoPage;
          }
          break;
        case 4:
          {
            params.paras.fuzzyQuery = this.searchKey;
            fun = intelligenceservice.trainInfoPage;
          }
          break;
        case 5:
          {
            params.paras.serviceName = this.searchKey;
            fun = intelligenceservice.planeInfoPage; // param
          }
          break;
        case 6:
          {
            params.paras.fuzzyQuery = this.searchKey;
            fun = intelligenceservice.shipInfoPage;
          }
          break;
        }
        if (this.defaultOrg) {
          params.paras.orgUuid = this.defaultOrg.id;
          fun(params)
            .then((res) => {
              if (res.success) {
                res.data.forEach((itm) => {
                  this.getItemCanEdit(itm);
                  itm.show = false;
                  itm.swiperDisabled = false;
                });
                resolve({
                  count: res.count,
                  data: res.data,
                });
              } else {
                this.$u.toast(res.msg);
                resolve({
                  count: 0,
                  data: [],
                });
              }
            })
            .catch((e) => {
              this.$u.toast("请求失败:" + e);
              resolve({
                count: 0,
                data: [],
              });
            });
        } else {
          this.getDefaultOrg("", "zm-all", (r) => {
            if (r) {
              this.defaultOrg = r;
              this.orgName = r.name;
              this.orgUuid = r.id;
              params.paras.orgUuid = this.defaultOrg.id;
            } else {
              params.paras.orgUuid = "";
            }
            fun(params)
              .then((res) => {
                if (res.success) {
                  res.data.forEach((itm) => {
                    this.getItemCanEdit(itm);
                    itm.show = false;
                    itm.swiperDisabled = false;
                  });
                  resolve({
                    count: res.count,
                    data: res.data,
                  });
                } else {
                  this.$u.toast(res.msg);
                  resolve({
                    count: 0,
                    data: [],
                  });
                }
              })
              .catch((e) => {
                this.$u.toast("请求失败:" + e);
                resolve({
                  count: 0,
                  data: [],
                });
              });
          });
        }
      });
    },
    getItemCanEdit(item){
      let disabled = true;
      let swiperOptions = [];
      if(item.ableOperate == 1){
        disabled = false;
        swiperOptions.push({
          text: '编辑',
          action:"edit",
          style: {
            backgroundColor: this.$theme.u_type_primary,
          },
        });
      }
      if(item.isActive == 1 || item.isActive == true){
        swiperOptions.push({
          text: '停用',
          action:"deactivate",
          style: {
            backgroundColor: this.$theme.u_type_error,
          },
        });
      } else {
        swiperOptions.push({
          text: '启用',
          action:"enable",
          style: {
            backgroundColor: this.$theme.u_type_success,
          },
        });
      }
      item.swiperOptions = swiperOptions;
      item.swiperDisabled = disabled;
    },
    swiperBtn (id,isActive,type) {
      if (isActive == 1 || isActive == true) {
        this.$modal.confirm("确定要停用吗？", () => {
          this.$modal.showLoading("停用中...");
          let params = { "uuid": id, "type": type, };
          intelligenceservice.changeActive(params).then(res => {
            if(res.success) {
              this.$u.toast("停用成功");
              this.getMescroll(0).triggerDownScroll();
            } else {
              this.$message.error(res.msg);
            }
          }).catch(err => {
            this.$modal.hideLoading();
            this.$u.toast("停用失败" + err);
          })
        });
      } else {
        this.$modal.confirm("确定要启用吗？", () => {
          this.$modal.showLoading("启用中...");
          let params = {"uuid":id,"type":type,};
          intelligenceservice.changeActive(params).then(res => {
            if(res.success) {
              this.$u.toast("启用成功");
              this.getMescroll(0).triggerDownScroll();
            } else {
              this.$message.error(res.msg);
            }
          }).catch(err => {
            this.$modal.hideLoading();
            this.$u.toast("启用失败" + err);
          })
        });
      }
    },
    swiperClick(index1, index) {
      let datas = this.$refs.mescrollItem0.listDatas;
      let cur = -1;
      for (let i = 0; i < datas.length; i++) {
        if (datas[i].uuid == index1) {
          cur = i;
          break;
        }
      }
      if (cur == -1) {
        this.$u.toast("选中数据无效");
        return;
      }
      switch (this.currentTab) {
      case 0:
        {
          if (index == 0) {
            // 修改
            if (datas[cur].ableOperate == 1) {
              let params = datas[cur];
              this.setRouteParam(params);
              this.$u.route({
                url: "/pages/intelligence/intelligenceFilesAdd/logisticsArchive",
                params: {
                  from: "edit",
                },
              });
              this.$refs.mescrollItem0.listDatas.map((val, idx) => {
                this.$refs.mescrollItem0.listDatas[idx].show = false;
              });
              return;
            } else {
              this.swiperBtn(datas[cur].uuid,datas[cur].isActive,"3");
            }
          } else if (index == 1) {
            // 启用停用
            this.swiperBtn(datas[cur].uuid,datas[cur].isActive,"3");
          }
        }
        break;
      case 1:
        {
          if (index == 0) {
            // 修改
            if (datas[cur].ableOperate == 1) {
              let params = datas[cur];
              this.setRouteParam(params);
              this.$u.route({
                url: "/pages/intelligence/intelligenceFilesAdd/expressArchive",
                params: {
                  from: "edit",
                },
              });
              this.$refs.mescrollItem0.listDatas.map((val, idx) => {
                this.$refs.mescrollItem0.listDatas[idx].show = false;
              });
              return;
            } else {
              this.swiperBtn(datas[cur].uuid,datas[cur].isActive,"4");
            }
          } else if (index == 1) {
            // 启用停用
            this.swiperBtn(datas[cur].uuid,datas[cur].isActive,"4");
          }
        }
        break;
      case 2:
        {
          if (index == 0) {
            if (datas[cur].ableOperate == 1) {
              let params = datas[cur];
              this.setRouteParam(params);
              this.$u.route({
                url: "/pages/intelligence/intelligenceFilesAdd/facsimileArchives",
                params: {
                  from: "edit",
                },
              });
              this.$refs.mescrollItem0.listDatas.map((val, idx) => {
                this.$refs.mescrollItem0.listDatas[idx].show = false;
              });
              return;
            } else {
              this.swiperBtn(datas[cur].uuid,datas[cur].isActive,"6");
            }
          } else if (index == 1) {
            // 启用停用
            this.swiperBtn(datas[cur].uuid,datas[cur].isActive,"6");
          }
        }
        break;
      case 3:
        {
          if (index == 0) {
            if (datas[cur].ableOperate == 1) {
              let params = datas[cur];
              this.setRouteParam(params);
              this.$u.route({
                url: "/pages/intelligence/intelligenceFilesAdd/roadtransportArchive",
                params: {
                  from: "edit",
                },
              });
              this.$refs.mescrollItem0.listDatas.map((val, idx) => {
                this.$refs.mescrollItem0.listDatas[idx].show = false;
              });
              return;
            } else {
              this.swiperBtn(datas[cur].uuid,datas[cur].isActive,"5");
            }
          } else if (index == 1) {
            // 启用停用
            this.swiperBtn(datas[cur].uuid,datas[cur].isActive,"5");
          }
        }
        break;
      case 4:
        {
          if (index == 0) {
            if (datas[cur].ableOperate == 1) {
              let params = datas[cur];
              this.setRouteParam(params);
              this.$u.route({
                url: "/pages/intelligence/intelligenceFilesAdd/railroadtransportArchive",
                params: {
                  from: "edit",
                },
              });
              this.$refs.mescrollItem0.listDatas.map((val, idx) => {
                this.$refs.mescrollItem0.listDatas[idx].show = false;
              });
              return;
            } else {
              this.swiperBtn(datas[cur].uuid,datas[cur].isActive,"8");
            }
          } else if (index == 1) {
            // 启用停用
            this.swiperBtn(datas[cur].uuid,datas[cur].isActive,"8");
          }
        }
        break;
      case 5:
        {
          if (index == 0) {
            if (datas[cur].ableOperate == 1) {
              let params = datas[cur];
              this.setRouteParam(params);
              this.$u.route({
                url: "/pages/intelligence/intelligenceFilesAdd/airtransportArchive",
                params: {
                  from: "edit",
                },
              });
              this.$refs.mescrollItem0.listDatas.map((val, idx) => {
                this.$refs.mescrollItem0.listDatas[idx].show = false;
              });
              return;
            } else {
              this.swiperBtn(datas[cur].uuid,datas[cur].isActive,"9");
            }
          } else if (index == 1) {
            // 启用停用
            this.swiperBtn(datas[cur].uuid,datas[cur].isActive,"9");
          }
        }
        break;
      case 6:
        {
          if (index == 0) {
            if (datas[cur].ableOperate == 1) {
              let params = datas[cur];
              this.setRouteParam(params);
              this.$u.route({
                url: "/pages/intelligence/intelligenceFilesAdd/waterwaytransportArchive",
                params: {
                  from: "edit",
                },
              });
              this.$refs.mescrollItem0.listDatas.map((val, idx) => {
                this.$refs.mescrollItem0.listDatas[idx].show = false;
              });
              return;
            } else {
              this.swiperBtn(datas[cur].uuid,datas[cur].isActive,"7");
            }
          } else if (index == 1) {
            // 启用停用
            this.swiperBtn(datas[cur].uuid,datas[cur].isActive,"7");
          }
        }
        break;
      }
      this.$refs.mescrollItem0.listDatas.map((val, idx) => {
        this.$refs.mescrollItem0.listDatas[idx].show = false;
      });
    },
    swiperOpen(index) {
      this.$refs.mescrollItem0.listDatas.map((val, idx) => {
        this.$refs.mescrollItem0.listDatas[idx].show = index == val.uuid;
      });
    },
    updateBackState(param) {
      this.getMescroll(0).triggerDownScroll();
    },
  },
};
</script>

<style scoped lang="scss">
@import "@/style/common/rightMenu.scss";
page{
  background-color: #f3f4f5;

}
.content {
  background-color: #f3f4f5;
  margin-top:35px;
}
.add-Mean {
  // #ifndef H5-DINGDING
  top: 88rpx;
  // #endif
  // #ifdef H5-DINGDING
  top: 0;
  // #endif
}
.nolicence-search-lay {
  // background-color: #fff;
  position: fixed;
  // #ifndef H5-DINGDING
  top: 44px;
  // #endif
  // #ifdef H5-DINGDING
  top: 0px;
  // #endif
  z-index: 999;
  width: 100%;
  overflow: hidden;
  background-color: #ffffff;
  border-bottom: 1px solid $u-border-color;
}
.nolicenSearch{
  padding: $cm-search-padding-row $cm-search-padding-column;

}
.key-search {
  width: 100%;
  margin: auto;
  padding-left: 2.5%;
  padding-right: 2.5%;
  line-height: 48px;
  border-bottom: 1px solid $u-border-color;
}
.pop-content-lay {
  padding: 20rpx;
  display: flex;
  height: 100%;
  flex-direction: column;
  justify-content: center;
}
.grid-text {
  font-size: 28rpx;
  margin-top: 4rpx;
  color: $u-type-info;
}
.list-item-root {
  border-bottom: 1px solid $u-border-color;
}

.list-lay {
  margin: 10px;
}
.list-item-root {
  margin: 10px;
  border-radius: 15px;
  overflow: hidden;
}
</style>
